<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$sql_sel = "SELECT m.cod_modulo, m.descr as m_descr FROM wfm_moduli m "
		." WHERE m.id_modulo = ".reqVarInt('idm');
$objDB->query($sql_sel,'selRec');
if(!$aRow = $objDB->fetch('selRec')) {
	backOnErr("id. errato");
}

$aVarExt['HtmlPopUpRegInp'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_inp','inp');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpRegVal'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_val','val');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
$aVarExt['HtmlPopUpRegOut'] = "<a href=\"javascript:apriPopUpInsReg('id_regola_out','out');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova regola\" border=\"0\"></a>";
	
reqDef('cod_pagina','');
reqDef('descr','');
reqDef('id_regola_inp',0);
reqDef('id_regola_val',0);
reqDef('id_regola_out',0);
reqDef('tipo','gener');
reqDef('tipo_inst','gener');
reqDef('nota','');

$aCamForm = array();
require("_inc/const.campiPag.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 


$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('annulla');

$sHtmlMod = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>\n";
$sHtmlMod .= getHtmlRigaRif();
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Modulo</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_modulo']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['m_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "</table>";

$aOpz['sottotit'] = $sHtmlMod;

testaHtml('Gestione work-flow','Modifica pagina',$msgPag,'wfma',$aOpz);
?>
<script language="JavaScript" type="text/javascript">
function apriPopUpInsReg(campo,tr){
  	url = "regInsPopUp.php?id=<?=reqvar('idm');?>&tr="+tr+"&campo="+campo+"&xsn=<?=XSN?>";
	window.open(url, 'nuovaPag','width=730,height=300,top=50,left=50,resizable=yes,scrollbars=yes');
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('idm');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Pagina</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'inp',$aVarExt);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_pagina'] = strtoupper(trim(reqVar('cod_pagina')));
	if (strlen(reqVar('cod_pagina'))< 4) {
		$msg .= "Codice obbligatorio di almeno 4 caratteri<br>";
	}
	if (preg_match("/[^A-Z0-9]/",reqVar('cod_pagina'))) {
		$msg = "Codice '".reqVar('cod_pagina')."' puo' contenere solo lettere numeri<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	
	$sqlSel = "SELECT cod_pagina FROM wfm_pagine  " 
			." WHERE cod_pagina = '".reqVarAS('cod_pagina')."'"
			." AND id_modulo = ".reqVarInt('idm');
			$objDB->exec($sqlSel,'chkDup');
	$aRow = $objDB->fetch('chkDup');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "INSERT INTO wfm_pagine SET id_modulo = ".reqVar('idm');
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			switch ($aCX[4]) {
				case 'date':
					$sSql .= "', ".$aCX[0]." = '".reqVarStr2YMD($aCX[0]);
					break;
				default:
					$sSql .= "', ".$aCX[0]." = '".reqVarAS($aCX[0]);
					break;
			}			
		}
	}
	$objDB->exec($sSql,'insRec');
	dbg_($sSql);
	header("Location: ".urlTorna(_t("pagina '".reqVarAS('cod_pagina')."' inserita")));
	exit();
}
